home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 June / PersonalComputerWorld-June2009-CoverdiscCD.iso / Software / Freeware / Firebug 1.3.3 / firebug-1.3.3-fx.xpi / content / firebug / consoleInjector.js < prev    next >
Encoding:
JavaScript  |  2009-02-19  |  12.1 KB  |  421 lines

  1. /* See license.txt for terms of usage */
  2.  
  3. //
  4. FBL.ns(function() { with (FBL) {
  5. // ************************************************************************************************
  6. // Constants
  7.  
  8. const Cc = Components.classes;
  9. const Ci = Components.interfaces;
  10. const versionChecker = CCSV("@mozilla.org/xpcom/version-comparator;1", Ci.nsIVersionComparator);
  11. const appInfo = CCSV("@mozilla.org/xre/app-info;1", Ci.nsIXULAppInfo);
  12.  
  13. top.Firebug.Console.injector = {
  14.  
  15.         isAttached: function(win)  // win needs to be a XPCSafeJSObjectWrapper
  16.         {
  17.             return (win._getFirebugConsoleElement ? true : false);
  18.         },
  19.         
  20.         attachIfNeeded: function(context, win)
  21.         {
  22.             if (this.isAttached(win))
  23.                 return true;
  24.             
  25.             this.attachConsoleInjector(context, win);
  26.             this.addConsoleListener(context, win);
  27.         
  28.             var attached = this.isAttached(win);
  29.             if (attached)
  30.             {
  31.                 // If the user has the cursor in the command line and reloads, the focus will already be there. issue 1339
  32.                 var isFocused = ($("fbLargeCommandLine").getAttribute("focused") == "true");
  33.                 isFocused = isFocused || ($("fbCommandLine").getAttribute("focused") == "true");
  34.                 if (isFocused)  
  35.                     setTimeout(top.FirebugChrome.onCommandLineFocus);
  36.             }
  37.             return attached;
  38.         },
  39.         
  40.         attachConsoleInjector: function(context, win)
  41.         {
  42.             var consoleInjection = this.getConsoleInjectionScript();  // Do it all here.
  43.            
  44.             Firebug.CommandLine.evaluateInSandbox(consoleInjection, context, null, win);
  45.  
  46.         },
  47.  
  48.         getConsoleInjectionScript: function() {
  49.             if (!this.consoleInjectionScript)
  50.             {
  51.                 var ff3 = versionChecker.compare(appInfo.version, "3.0*") >= 0;
  52.  
  53.                 // There is a "console" getter defined for FF3.
  54.                 var script = "";
  55.                 if (ff3)
  56.                 {
  57.                     script += "window.__defineGetter__('console', function() {\n";
  58.                     script += " return window.loadFirebugConsole(); })\n\n";
  59.                 }
  60.  
  61.                 script += "window.loadFirebugConsole = function() {\n";
  62.                 script += "window._firebug =  new _FirebugConsole();";
  63.                 // If not ff3 initialize "console" property.
  64.                 if (!ff3)
  65.                     script += " window.console = window._firebug;\n";
  66.  
  67.                 script += " return window._firebug };\n";
  68.                 
  69.                 var theFirebugConsoleScript = getResource("chrome://firebug/content/consoleInjected.js");
  70.                 script += theFirebugConsoleScript;
  71.                 
  72.                 if (!ff3)
  73.                     script += " window.loadFirebugConsole();\n";
  74.                 
  75.                 this.consoleInjectionScript = script;
  76.             }
  77.             return this.consoleInjectionScript;
  78.         },
  79.  
  80.     forceConsoleCompilationInPage: function(context, win)
  81.     {
  82.         if (!win)
  83.         {
  84.             return;
  85.         }
  86.         
  87.         var consoleForcer = "window.loadFirebugConsole();";
  88.         
  89.         if (context.stopped)
  90.             Firebug.Console.injector.evaluateConsoleScript(context);  // todo evaluate consoleForcer on stack
  91.         else
  92.             Firebug.CommandLine.evaluateInSandbox(consoleForcer, context, null, win);
  93.     },
  94.  
  95.     evaluateConsoleScript: function(context)
  96.     {
  97.         var scriptSource = this.getConsoleInjectionScript(); // TODO XXXjjb this should be getConsoleInjectionScript
  98.         Firebug.Debugger.evaluate(scriptSource, context);
  99.     },
  100.  
  101.     addConsoleListener: function(context, win)
  102.     {
  103.         if (!context.activeConsoleHandlers)  // then we have not been this way before
  104.             context.activeConsoleHandlers = [];  
  105.         else
  106.         {   // we've been this way before...
  107.             for (var i=0; i<context.activeConsoleHandlers.length; i++)
  108.             {
  109.                 if (context.activeConsoleHandlers[i].window == win)
  110.                 {
  111.                     context.activeConsoleHandlers[i].detach();
  112.                     context.activeConsoleHandlers.splice(i,1);
  113.                 }
  114.             }   
  115.         }
  116.         
  117.         // We need the element to attach our event listener.
  118.         var element = Firebug.Console.getFirebugConsoleElement(context, win);
  119.         element.setAttribute("FirebugVersion", Firebug.version); // Initialize Firebug version.
  120.         
  121.         var handler = new FirebugConsoleHandler(context, win);
  122.         handler.attachTo(element);
  123.         
  124.         context.activeConsoleHandlers.push(handler);
  125.  
  126.         return true;
  127.     }
  128. }
  129.  
  130. var total_handlers = 0;
  131. function FirebugConsoleHandler(context, win)
  132. {
  133.     this.window = win;
  134.     
  135.     this.attachTo = function(element)
  136.     {
  137.         this.element = element;
  138.         // When raised on our injected element, callback to Firebug and append to console
  139.         this.boundHandler = bind(this.handleEvent, this);
  140.         this.element.addEventListener('firebugAppendConsole', this.boundHandler, true); // capturing
  141.     };
  142.     
  143.     this.detach = function()
  144.     {
  145.         this.element.removeEventListener('firebugAppendConsole', this.boundHandler, true);
  146.     };
  147.     
  148.     this.handler_name = ++total_handlers;
  149.     this.handleEvent = function(event)
  150.     {
  151.         if (!Firebug.CommandLine.CommandHandler.handle(event, this, win))
  152.         {
  153.             var methodName = event.target.getAttribute("methodName");
  154.             Firebug.Console.log($STRF("console.MethodNotSupported", [methodName]));
  155.         }
  156.     };
  157.  
  158.     this.firebug = Firebug.version;
  159.  
  160.     this.init = function()
  161.     {
  162.         var consoleElement = win.document.getElementById('_firebugConsole');
  163.         consoleElement.setAttribute("FirebugVersion", Firebug.version);
  164.     };
  165.  
  166.     this.log = function()
  167.     {
  168.         logFormatted(arguments, "log");
  169.     };
  170.  
  171.     this.debug = function()
  172.     {
  173.         logFormatted(arguments, "debug", true);
  174.     };
  175.  
  176.     this.info = function()
  177.     {
  178.         logFormatted(arguments, "info", true);
  179.     };
  180.  
  181.     this.warn = function()
  182.     {
  183.         logFormatted(arguments, "warn", true);
  184.     };
  185.  
  186.     this.error = function()
  187.     {
  188.         Firebug.Errors.increaseCount(context);
  189.         logFormatted(arguments, "error", true);
  190.     };
  191.  
  192.     this.assert = function(x)
  193.     {
  194.         if (!x)
  195.         {
  196.             var rest = [];
  197.             for (var i = 1; i < arguments.length; i++)
  198.                 rest.push(arguments[i]);
  199.             logAssert(rest);
  200.         }
  201.     };
  202.  
  203.     this.dir = function(o)
  204.     {
  205.         Firebug.Console.log(o, context, "dir", Firebug.DOMPanel.DirTable);
  206.     };
  207.  
  208.     this.dirxml = function(o)
  209.     {
  210.         if (o instanceof Window)
  211.             o = o.document.documentElement;
  212.         else if (o instanceof Document)
  213.             o = o.documentElement;
  214.  
  215.         Firebug.Console.log(o, context, "dirxml", Firebug.HTMLPanel.SoloElement);
  216.     };
  217.  
  218.     this.trace = function()
  219.     {
  220.         var trace = getJSDUserStack();
  221.         Firebug.Console.log(trace, context, "stackTrace");
  222.     };
  223.  
  224.     this.group = function()
  225.     {
  226.         var sourceLink = getStackLink();
  227.         Firebug.Console.openGroup(arguments, null, "group", null, false, sourceLink);
  228.     };
  229.  
  230.     this.groupEnd = function()
  231.     {
  232.         Firebug.Console.closeGroup(context);
  233.     };
  234.  
  235.     this.groupCollapsed = function()
  236.     {
  237.         var sourceLink = getStackLink();
  238.         // noThrottle true is probably ok, openGroups will likely be short strings.
  239.         var row = Firebug.Console.openGroup(arguments, null, "group", null, true, sourceLink);
  240.         removeClass(row, "opened");
  241.     };
  242.  
  243.     this.profile = function(title)
  244.     {
  245.         Firebug.Profiler.startProfiling(context, title);
  246.     };
  247.  
  248.     this.profileEnd = function()
  249.     {
  250.         Firebug.Profiler.stopProfiling(context);
  251.     };
  252.  
  253.     this.count = function(key)
  254.     {
  255.         var frameId = FBL.getStackFrameId();
  256.         if (frameId)
  257.         {
  258.             if (!context.frameCounters)
  259.                 context.frameCounters = {};
  260.  
  261.             if (key != undefined)
  262.                 frameId += key;
  263.  
  264.             var frameCounter = context.frameCounters[frameId];
  265.             if (!frameCounter)
  266.             {
  267.                 var logRow = logFormatted(["0"], null, true, true);
  268.  
  269.                 frameCounter = {logRow: logRow, count: 1};
  270.                 context.frameCounters[frameId] = frameCounter;
  271.             }
  272.             else
  273.                 ++frameCounter.count;
  274.  
  275.             var label = key == undefined
  276.                 ? frameCounter.count
  277.                 : key + " " + frameCounter.count;
  278.  
  279.             frameCounter.logRow.firstChild.firstChild.nodeValue = label;
  280.         }
  281.     };
  282.  
  283.     this.clear = function()
  284.     {
  285.         Firebug.Console.clear(context);
  286.     };
  287.  
  288.     this.time = function(name, reset)
  289.     {
  290.         if (!name)
  291.             return;
  292.  
  293.         var time = new Date().getTime();
  294.  
  295.         if (!this.timeCounters)
  296.             this.timeCounters = {};
  297.  
  298.         if (!reset && this.timeCounters[name])
  299.             return;
  300.  
  301.         this.timeCounters[name] = time;
  302.     };
  303.  
  304.     this.timeEnd = function(name)
  305.     {
  306.         var time = new Date().getTime();
  307.  
  308.         if (!this.timeCounters)
  309.             return;
  310.  
  311.         var timeCounter = this.timeCounters[name];
  312.         if (timeCounter)
  313.         {
  314.             var diff = time - timeCounter;
  315.             var label = name + ": " + diff + "ms";
  316.  
  317.             this.info(label);
  318.  
  319.             delete this.timeCounters[name];
  320.         }
  321.         return diff;
  322.     };
  323.  
  324.     // These functions are over-ridden by commandLine
  325.     this.evaluated = function(result, context)
  326.     {
  327.         Firebug.Console.log(result, context);
  328.     };
  329.     this.evaluateError = function(result, context)
  330.     {
  331.         Firebug.Console.log(result, context, "errorMessage");
  332.     };
  333.  
  334. /*
  335.     this.addTab = function(url, title, parentPanel)
  336.     {
  337.         context.chrome.addTab(context, url, title, parentPanel);
  338.     };
  339.  
  340.     this.removeTab = function(url)
  341.     {
  342.         context.chrome.removeTab(context, url);
  343.     };
  344. */
  345.  
  346.     // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  347.  
  348.     function logFormatted(args, className, linkToSource, noThrottle)
  349.     {
  350.         var sourceLink = linkToSource ? getStackLink() : null;
  351.         return Firebug.Console.logFormatted(args, context, className, noThrottle, sourceLink);
  352.     }
  353.  
  354.     function logAssert(args)
  355.     {
  356.         Firebug.Errors.increaseCount(context);
  357.  
  358.         if (!args || !args.length || args.length == 0)
  359.             var msg = [FBL.$STR("Assertion")];
  360.         else
  361.             var msg = args[0];
  362.  
  363.         var sourceName = win.location;
  364.         var lineNumber = 0;
  365.         var trace = getJSDUserStack();
  366.         if (trace && trace.frames && trace.frames[0])
  367.         {
  368.             var frame = trace.frames[0];
  369.             sourceName = normalizeURL(frame.script.fileName);
  370.             lineNumber = frame.line;
  371.         }
  372.  
  373.         var errorObject = new FBL.ErrorMessage(msg, sourceName,
  374.                         lineNumber, "", "assert", context, trace);
  375.  
  376.         var row = Firebug.Console.log(errorObject, context, "errorMessage", null, true); // noThrottle
  377.         row.scrollIntoView();
  378.     }
  379.  
  380.     function getComponentsStackDump()
  381.     {
  382.         // Starting with our stack, walk back to the user-level code
  383.         var frame = Components.stack;
  384.         var userURL = win.location.href.toString();
  385.         
  386.         while (frame && FBL.isSystemURL(frame.filename) )
  387.             frame = frame.caller;
  388.  
  389.         return frame;
  390.     }
  391.  
  392.     function getStackLink()
  393.     {
  394.         return FBL.getFrameSourceLink(getComponentsStackDump());
  395.     }
  396.  
  397.     function getJSDUserStack()
  398.     {
  399.         var trace = FBL.getCurrentStackTrace(context);
  400.  
  401.         var frames = trace ? trace.frames : null;
  402.         if (frames && (frames.length > 0) )
  403.         {
  404.             var bottom = frames.length - 1;
  405.             for (var i = 0; i < frames.length; i++)
  406.             {
  407.                 if (frames[bottom - i].href.indexOf("chrome:") == 0) break;
  408.                 var fn = frames[bottom - i].fn + "";
  409.                 if (fn && (fn.indexOf("_firebugEvalEvent") != -1) ) break;
  410.             }
  411.  
  412.             trace.frames = trace.frames.slice(bottom - i + 1);
  413.             return trace;
  414.         }
  415.         else
  416.             return "Firebug failed to get stack trace with any frames";
  417.     }
  418. }
  419.  
  420. }});
  421.